package vip.meeet;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class ZookeeperStart {

	private static final int TIME_OUT = 3000;
	
	private static final String HOST = "localhost:2181";

	public static void main(String[] args) throws Exception {

		ZooKeeper zookeeper = new ZooKeeper(HOST, TIME_OUT, null);
		if (zookeeper.exists("/test", false) == null) {
			String actualPath = zookeeper.create("/test", "znode1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			System.out.println(actualPath);
		}
		
		byte [] node = zookeeper.getData("/test", false, null);
		System.out.println(new String(node));

		Stat status = zookeeper.exists("/test", false);
		System.out.println("节点状态：" + status);
		
		String data = "zNode2";
		zookeeper.setData("/test", data.getBytes(), -1);

		node = zookeeper.getData("/test", false, null);
		System.out.println(new String(node));

		
		zookeeper.delete("/test", -1);

		status = zookeeper.exists("/test", false);
		System.out.println("节点状态：" + status);

		zookeeper.close();
	}

}
